//
//  JAHomeController.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/2.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit
import SDWebImage

fileprivate let identifer = "homeCell"
fileprivate let retweetedIdentifier = "retweetedIdentifier"

class JAHomeController: JARootController {
    
    var statusListViewModel: JAStatusListViewModel = JAStatusListViewModel()
    var  dataSourceArr: [JAStatusViewModel] = []
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //加载tableView
        self.setupUI()
        
        //获取数据.判断用户是否是登录状态
        if JAUserAccount.shared.isLogin {
            loadData()
        }
        
        //接收微博首页的图片点击的通知
        NotificationCenter.default.addObserver(self, selector: #selector(showPhotoBroser(notification:)), name: pictureViewClickedNotification, object: nil)

    }
}
// MARK: - 事件处理
extension JAHomeController {
    func showPhotoBroser(notification: Notification) {
        let usrinfo = notification.userInfo
        
        let index = usrinfo?["index"] as! Int
        let pic_urls = usrinfo?["urls"] as! [String]
        
        let photoBroser = JAPhotoBroserController(index: index, pic_urls: pic_urls)
        
        present(photoBroser, animated: false, completion: nil)
    }
}
// MARK: - 设置UI
extension JAHomeController {
    fileprivate func setupUI () {
        
        //注册cell
        tableView.register(JAStatusCell.self, forCellReuseIdentifier: identifer)
        tableView.register(JARetweetedStatusCell.self, forCellReuseIdentifier: retweetedIdentifier)
        
        //行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        

    }
}

// MARK: - 加载数据
extension JAHomeController {
    override func loadData() {
        
        var isPullDown = true
        
        if refreshHeader.isRefreshing() {
            isPullDown = true
        } else {
            isPullDown = false
        }
        
        
        let updateUICloure = {
            (success: Bool) in
            if success {
                self.tableView.reloadData()
                
                //判断是上拉, 还是下拉, 结束菊花转动的状态
                if isPullDown == true {
                    self.refreshHeader.endRefreshing()
                } else {
                    self.refreshFooter.endRefreshing()
                }
        } else {
            
            if isPullDown == true {
                self.refreshHeader.endRefreshing()
            } else {
                self.refreshFooter.endRefreshing()
            }
        }
    }
      //调用viewModel来获取数据
        statusListViewModel.loadData(isPullDown: isPullDown, callBack: updateUICloure)
  }
}

// MARK: - tableView的数据源方法
extension JAHomeController {
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let viewModel = statusListViewModel.dataSourceArr[indexPath.row]
            
            //如果有转发微博
            if let _ = viewModel.statusModel.retweeted_status {
                let cell = tableView.dequeueReusableCell(withIdentifier: retweetedIdentifier, for: indexPath) as! JARetweetedStatusCell
                
                cell.statusViewModel = viewModel
                
                return cell
            }
            else {
                
                //如果没有转发微博
                let cell = tableView.dequeueReusableCell(withIdentifier: identifer, for: indexPath) as! JAStatusCell
        
                cell.statusViewModel = viewModel
                
                return cell
            }
    
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            return statusListViewModel.dataSourceArr.count
        }
   }
